//
//  RegistViewController.m
//  Travel assistant
//
//  Created by 王京 on 16/3/10.
//  Copyright © 2016年 旅游助手. All rights reserved.
//

#import "RegistViewController.h"
#import "LoginViewController.h"
#import "MyViewController.h"
#import "DataHelper.h"

@interface RegistViewController ()

@property (nonatomic,strong) UITextField *phoneNumberText;

@property (nonatomic,strong) UITextField *codeText;

@property (nonatomic,strong) UITextField *passWordText;

@property (nonatomic,strong) UIButton *getBtn;

@end

@implementation RegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"注册会员";
    
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:(UIBarButtonItemStylePlain) target:self action:@selector(rightBtnAction)];
    
    self.navigationItem.rightBarButtonItem = rightBtn;
    
    [self add];
    
    
    
    // Do any additional setup after loading the view.
}


- (void)add{
    
    self.phoneNumberText = [[UITextField alloc]init];
    
    self.phoneNumberText.frame = CGRectMake(40, 84, CGRectGetWidth(self.view.frame)-40, 40);
    
    self.phoneNumberText.placeholder = @"设置用户名";
    
    [self.view addSubview:self.phoneNumberText];
    
    
    self.codeText = [[UITextField alloc]init];
    
    self.codeText.frame = CGRectMake(40, CGRectGetMaxY(self.phoneNumberText.frame), CGRectGetWidth(self.view.frame)-40, 40);
    
    self.codeText.placeholder = @"设置验证邮箱";
    
    [self.view addSubview:self.codeText];
    
    self.passWordText = [[UITextField alloc]init];
    
    self.passWordText.frame = CGRectMake(40, CGRectGetMaxY(self.codeText.frame), CGRectGetWidth(self.phoneNumberText.frame), 40);
    
    self.passWordText.secureTextEntry = YES;
    
    self.passWordText.placeholder = @"设置密码";
    
    [self.view addSubview:self.passWordText];
    
    
    
    
}


-(void)rightBtnAction{
    AVUser *user = [AVUser user];// 新建 AVUser 对象实例
    user.username = self.phoneNumberText.text;// 设置用户名
    user.password =  self.passWordText.text;// 设置密码
        user.email = self.codeText.text;// 设置邮箱
    
    [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            
            NSLog(@"注册成功");
            
            
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"注册"
                                                                           message:@"注册成功"
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                                  handler:^(UIAlertAction * action) {
                                                                      HELP.userName = self.phoneNumberText.text;
                                                                      
                                                                      HELP.placeHoderImageV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"9.jpg"]];
                                                                  
                                                                      HELP.registToView = YES;
                                                                      
                                                                      AVUser *user = [AVUser user];
                                                                      user.username = self.phoneNumberText.text;
                                                                      user.password = self.passWordText.text;
                                                                      user.email = self.codeText.text;
                                                                      
                                                                      AVObject *objectUser = [AVObject objectWithClassName:@"UserInformation"];
                                                                      [objectUser setObject:self.self.phoneNumberText.text forKey:@"userName"];
                                                                      UIImage *userIcon = [UIImage imageNamed:@"UserCenter_HeadPortrait"];
                                                                      NSData *dataIcon = UIImagePNGRepresentation(userIcon);
                                                                      AVFile *fileUserIcon = [AVFile fileWithData:dataIcon];
                                                                      [objectUser setObject:fileUserIcon forKey:@"userImage"];
                                                                      [objectUser saveInBackground];
                                                                      
                                                                      
                                                                      MyViewController *my = [[MyViewController alloc]init];
                                                                      
                                                                      [self.navigationController pushViewController:my animated:YES];
                                                                  
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
            
            
        } else {
            // 失败的原因可能有多种，常见的是用户名已经存在。
            NSLog(@"注册失败");
            
            if (error.code == 202) {
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"用户名已存在" message:@"" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    self.phoneNumberText.text = nil;
                    self.passWordText.text = nil;
                    self.codeText.text = nil;
                }];
                [alertController addAction:alertAction];
                [self presentViewController:alertController animated:YES completion:nil];
            }else if (error.code == 205) {
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"邮箱地址无效" message:@"" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    self.phoneNumberText.text = nil;
                    self.passWordText.text = nil;
                    self.codeText.text = nil;
                }];
                [alertController addAction:alertAction];
                [self presentViewController:alertController animated:YES completion:nil];

            }else if (error.code == 125){
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"邮箱地址无效" message:@"" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    self.phoneNumberText.text = nil;
                    self.passWordText.text = nil;
                    self.codeText.text = nil;
                }];
                [alertController addAction:alertAction];
                [self presentViewController:alertController animated:YES completion:nil];
            }else{
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"注册失败" message:@"" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    self.phoneNumberText.text = nil;
                    self.passWordText.text = nil;
                    self.codeText.text = nil;
                }];
                [alertController addAction:alertAction];
                [self presentViewController:alertController animated:YES completion:nil];

            }
        }
    }];
    

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
